package com.yuanfengerp.inventorycheck.inadvancecheck.service.impl;

import com.yuanfengerp.inventorycheck.inadvancecheck.dao.IInadvancecheckDao;
import com.yuanfengerp.inventorycheck.inadvancecheck.model.Inadvancecheck;
import com.yuanfengerp.inventorycheck.inadvancecheck.service.IInadvancecheckService;
import com.yuanfengerp.publicUtil.Result;
import com.yuanfengerp.publicUtil.SnowflakeIdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class InadvancecheckImpl implements IInadvancecheckService {
    @Autowired
    IInadvancecheckDao idac;

    @Override
    public Result<List<Inadvancecheck>> selectInadvanceAndshopAll(String document_ID) {
        List<Inadvancecheck> inadvancechecks = idac.selectInadvanceAndshopAll(document_ID);
//        System.out.println(inadvancechecks.toString());
        if (!(inadvancechecks.isEmpty()))
            return Result.success(inadvancechecks);
        return Result.success(inadvancechecks);
    }

    @Override
    public Result<List<Inadvancecheck>> selectInadvanceAll(Map<String, Object> params) {
        params.put("pagenum",((int) params.get("pagenum")-1) * (int) params.get("num"));
        List<Inadvancecheck> inadvancechecks = idac.selectInadvanceAll(params);
        if (!(inadvancechecks.isEmpty()))
            return Result.success(inadvancechecks, idac.Count());
        return Result.failure(400,"没有这条信息");
    }

    @Override
    @Transactional
    public Result<String> addInadvance(Inadvancecheck inadvancecheck) {
        SnowflakeIdWorker idWorker = new SnowflakeIdWorker(0);
        inadvancecheck.setDocument_ID(idWorker.nextId());
        inadvancecheck.setOperation_time(new Date(System.currentTimeMillis()));
        inadvancecheck.getCheckshops().forEach(checkshop -> {
            checkshop.setPm_id(inadvancecheck.getDocument_ID());
            checkshop.setPm_category(inadvancecheck.getWarehouse_name());
        });
        int i = idac.insertInadvance(inadvancecheck);
        inadvancecheck.getCheckshops().forEach(checkshop -> {
            idac.addshop(checkshop);
        });
        if (i == 1)
            return Result.success("新增成功");
        return Result.failure(500,"新增失败");
    }

    @Override
    @Transactional
    public Result<String> updateInadvance(Inadvancecheck inadvancecheck) {
        int i = idac.updateInadvance(inadvancecheck);
        //inadvancecheck的document_ID与checkshop的pm_id必须相同
        inadvancecheck.getCheckshops().forEach(checkshop -> {
            checkshop.setPm_id(inadvancecheck.getDocument_ID());
            checkshop.setPm_category(inadvancecheck.getWarehouse_name());
        });
        inadvancecheck.getCheckshops().forEach(checkshop -> {
            idac.updateShop(checkshop);
        });
        if (i == 1)
            return Result.success("修改成功");
        return Result.failure(500,"修改失败");
    }

    @Override
    @Transactional
    public Result<String> deleteInadvance(String document_ID) {
        int i = idac.deleteInadvance(document_ID);
        int i1 = idac.deleteShop(document_ID);
        if (i > 0 && i1 > 0)
            return Result.success("删除成功");
        return Result.failure(500,"删除失败");
    }


}
